/*
 * Copyright (c) 2012 Robert Burrell Donkin robertburrelldonkin.name
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */
package sekhmet.audio.data;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.nio.ByteBuffer;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.SourceDataLine;

public class AudioData {

	private final ByteBuffer buffer;
	private final AudioFormat format;
	private final long startFrame;
	private final long startMillis;
	private final long numberOfFrames;
	private final long durationMillis;
	
	public AudioData(ByteBuffer buffer, AudioFormat format, long startFrame,
			long startMillis, long numberOfFrames, long durationMillis) {
		super();
		this.buffer = buffer;
		this.format = format;
		this.startFrame = startFrame;
		this.startMillis = startMillis;
		this.numberOfFrames = numberOfFrames;
		this.durationMillis = durationMillis;
	}

	public AudioFormat getFormat() {
		return format;
	}
	
	public void writeTo(final SourceDataLine line) {
		line.write(buffer.array(), buffer.arrayOffset(), buffer.limit());
	}
	
	public long getStartMillis() {
		return startMillis;
	}

	public long getDurationMillis() {
		return durationMillis;
	}

	public long getStartFrame() {
		return startFrame;
	}

	public long getNumberOfFrames() {
		return numberOfFrames;
	}

	public void trace(PrintStream out) {
		out.print("[start: " + getStartFrame() + " (" + getStartMillis() + "), duration:" + getNumberOfFrames() + " (" + getDurationMillis() + "), bytes:" + buffer.remaining() + "]");
	}

	public InputStream inputStream() {
		return new ByteArrayInputStream(buffer.array(), 0 , buffer.limit());
	}
}
